
package com.xin.di.uav.applet.mapper;

import com.xin.di.uav.operate.po.ServiceAreaPO;
import com.xin.di.uav.user.po.UserPO;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface NewUserMapper {
    @Insert({"<script>",
            "INSERT INTO user (id, service_region, latitude, longitude, detailed_address, ",
            "person_in_charge, contact_number,  status)",
            "VALUES (#{id}, #{serviceRegion}, #{latitude}, #{longitude}, #{detailedAddress}, ",
            "#{personInCharge}, #{contactNumber},  #{status})",
            "</script>"})
    int insert(ServiceAreaPO serviceArea);

    int deleteById(String id);

    // 动态字段更新（只更新非空字段）
    @Update({
            "<script>",
            "UPDATE user ",
            "<set>",
            "    <if test='status != null'>status = #{status},</if>",
            "    updated_at = NOW()",

            "</set>",
            "WHERE id = #{id}",
            "</script>"
    })
    int updateById(UserPO userPO);

    @Select({"<script>",
            "select id, phone, country_code, platform_code, display_name, role, avatar, status, created_at, updated_at \n" +
                    "from user where id =#{id} ",
            "</script>"})
    UserPO selectById(String id);

    @Select({"<script>",
            "select id, phone, country_code, platform_code, display_name, role, avatar, status, created_at, updated_at \n" +
                    "from user where id in  ",
            "       <foreach item=\"id\" collection=\"ids\" open=\"(\" separator=\",\" close=\")\">\n" +
                    "            #{id}\n" +
                    "        </foreach>" +
            "</script>"})
    List<UserPO> selectByIds(@Param("ids") List<String> ids);


    @Select({"<script>",
            "select count(1) \n" +
                    "from user WHERE 1=1 ",
            "    <if test='phone != null and phone !=\"\" '> AND phone = #{phone}</if>",
            "<if test='startTime != null '>" +
                    " AND created_at &gt; #{startTime} AND created_at &lt; #{endTime} </if>",
            "</script>"})
    int count(@Param("phone") String phone, @Param("startTime") Date startTime, @Param("endTime") Date endTime);


    @Select({"<script>",
            "select id, phone, display_name, role, status, created_at, updated_at \n" +
                    " from user WHERE 1=1 " +
                    "    <if test='phone != null and phone !=\"\" '> AND phone = #{phone}</if>",
            "<if test='startTime != null '>" +
                    " AND created_at &gt; #{startTime} AND created_at &lt; #{endTime} </if>",
            " order by created_at desc   limit #{offset},#{pageSize} ",
            "</script>"})
    List<UserPO> selectList(@Param("offset") int offset, @Param("pageSize") int pageSize, @Param("phone") String phone
            , @Param("startTime") Date startTime, @Param("endTime") Date endTime);
}
